package com.liziba.reflect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * <p>
 *
 * </p>
 *
 * @Author: Liziba
 * @Date: 2021/4/25 21:30
 */
public class TestReflect {

    public static void main(String[] args) {
        try {
            // 获取Person的Class对象
            Class<?> clazz = Class.forName("com.liziba.reflect.Person");
            Person person = (Person)clazz.newInstance();
            // 获取Person类的所有成员属性信息
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                System.out.println(field);
            }
            // 获取Person类的所有方法信息
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                System.out.println(method);
            }

//            Class<?> clazz = TestReflect.class.getClassLoader().loadClass("com.liziba.reflect.Person");

            int modifiers = clazz.getModifiers();
            System.out.println(modifiers);

            // 获取Person类的所有构造方法信息
            Constructor<?>[] constructors = clazz.getDeclaredConstructors();
            for (Constructor<?> constructor : constructors) {
                System.out.println(constructor);
            }

            Constructor<?> constructor = clazz.getConstructor(String.class, int.class, String.class, int.class);
            constructor.newInstance("李子捌", 1, "深圳宝安", 23);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
